package com.basker.pisces.core.operator;

import org.springframework.util.StringUtils;

import com.basker.pisces.core.Element;
import com.basker.pisces.utils.ReflectionUtils;

public abstract class CompareOperator extends Element {

    private String operatorId;
    private CompareOperatorGroup group;
    private String operatorHandlerClass;
    private String groupId;

    private volatile ICompareOperatorHandler handler;

    /**
     * 获取所属符号组
     *
     * @return
     */
    public CompareOperatorGroup getGroup() {
        return group;
    }

    public String getGroupId() {
        return groupId;
    }

    public final ICompareOperatorHandler getOperatorHandler() {
        if (this.handler == null) {
            synchronized (this) {
                if (this.handler == null) {
                    this.handler = this.createOperatorHandler();
                }
            }
        }

        return handler;
    }

    public String getOperatorHandlerClass() {
        return operatorHandlerClass;
    }

    public String getOperatorId() {
        return operatorId;
    }

    public void setGroup(CompareOperatorGroup group) {
        this.group = group;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public void setOperatorHandlerClass(String operatorHandlerClass) {
        this.operatorHandlerClass = operatorHandlerClass;
    }

    public void setOperatorId(String operatorId) {
        this.operatorId = operatorId;
    }

    protected ICompareOperatorHandler createOperatorHandler() {
        String className = getOperatorHandlerClass();

        if (!StringUtils.isEmpty(className)) {
            return ReflectionUtils.createNewInstance(className);
        }

        return null;
    }
}
